package web

import (
	"fmt"
	"github.com/zachary/tbox/internal/svc"
	"github.com/zachary/tbox/internal/web/handler/demo"
	"github.com/zachary/tbox/internal/web/handler/favicon"
	"github.com/zachary/tbox/internal/web/handler/login"
	"github.com/zachary/tbox/internal/web/handler/student"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest"
	"net/http"
)

func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
	server.AddRoute(rest.Route{
		Method: http.MethodGet,
		Path:   "/",
		Handler: func(writer http.ResponseWriter, request *http.Request) {
			url := fmt.Sprintf("http://%s%sdemo/", request.Host, request.RequestURI)
			logx.Info("Redirect [/] to [/demo/]")
			http.Redirect(writer, request, url, http.StatusMovedPermanently)
		},
	})

	// favicon
	server.AddRoutes(favicon.GetNoAuthRoutes(serverCtx))

	// -------------------Demo start-------------------
	// 需要鉴权
	server.AddRoutes(demo.GetAuthRoutes(serverCtx),
		rest.WithPrefix("/demo"), rest.WithJwt(serverCtx.Config.Auth.AccessSecret))
	// 无需鉴权
	server.AddRoutes(demo.GetNoAuthRoutes(serverCtx), rest.WithPrefix("/demo"))
	// -------------------Demo end-------------------

	// -------------------登录 start-------------------
	// 无需鉴权
	server.AddRoutes(login.GetNoAuthRoutes(serverCtx), rest.WithPrefix("/login"))
	// -------------------登录 end-------------------

	// -------------------Student start-------------------
	server.AddRoutes(student.AuthRoutes(),
		rest.WithPrefix("/student"), rest.WithJwt(serverCtx.Config.Auth.AccessSecret))
	// -------------------Student start-------------------
}
